package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
        //	创建List<JSONObject> 对象
        List<JSONObject> list = new ArrayList<>();
        //查看所有分类
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
        //需要获取到一级分类id,一级分类名; 用分组技术：使用一级分类Id 当做分组的条件,分完组之后，将一级分类id作为key,一级分类数据集合作为value Map<Long,List<BaseCategoryView>>
        Map<Long, List<BaseCategoryView>> listMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //使用迭代器进行遍历map集合获取
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = listMap.entrySet().iterator();
        while (iterator.hasNext()) {
            //next 获取当前数据
            Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
            Long category1Id = entry.getKey();
            List<BaseCategoryView> categoryViewList1 = entry.getValue();
            //创建一个一级分类对象
            JSONObject category1 = new JSONObject();
            category1.put("categoryId", category1Id);
            category1.put("categoryName", categoryViewList1.get(0).getCategory1Name());
            //获取二级分类数据
            Map<Long, List<BaseCategoryView>> listMap1 = categoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //获取二级数据
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = listMap1.entrySet().iterator();
            //创建集合当前一级id有3个二级分类
            List<JSONObject> categroyChild2List = new ArrayList<>();
            while (iterator1.hasNext()) {
                //	获取map的entry对象
                Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
                //获取二级分类集合数据
                Long category2Id = entry1.getKey();
                List<BaseCategoryView> categoryViewList2 = entry1.getValue();
                JSONObject category2 = new JSONObject();
                category2.put("categoryId", category2Id);
                category2.put("categoryName", categoryViewList2.get(0).getCategory2Name());
                categroyChild2List.add(category2);
                //获取二级分类下获取三级id
                List<JSONObject> categoryChild3List = categoryViewList2.stream().map(baseCategoryView -> {
                    JSONObject category3 = new JSONObject();
                    category3.put("categoryId", baseCategoryView.getCategory3Id());
                    category3.put("categoryName", baseCategoryView.getCategory3Name());
                    //	返回对象
                    return category3;
                }).collect(Collectors.toList());
                //把三级分类的对象添加到二级分类里
                category2.put("categoryChild", categoryChild3List);
            }
            //把二级分类的集合添加到一级分类里
            category1.put("categoryChild", categroyChild2List);
            //把一级分类放到集合里
            list.add(category1);
        }

        return list;
    }

    @Override
    public JSONObject getBaseCategoryList(Long category1Id) {
        //创建对象
        JSONObject category1 = new JSONObject();
        category1.put("categoryId", category1Id);
        //找二三级分类的数据
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
        //以一级id进行分组
        Map<Long, List<BaseCategoryView>> listMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //迭代器遍历
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = listMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
            List<BaseCategoryView> categoryViewList = entry.getValue();
            category1.put("categoryName", categoryViewList.get(0).getCategory1Name());
            //获取二级分类数据
            ArrayList<JSONObject> categorxyChild2List = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> listMap1 = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = listMap1.entrySet().iterator();
            while (iterator1.hasNext()) {
                Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
                List<BaseCategoryView> categoryViewList1 = entry1.getValue();
                //获取二级分类对象
                JSONObject category2 = new JSONObject();
                category2.put("categoryId", entry1.getKey());
                category2.put("categoryName", categoryViewList1.get(0).getCategory2Name());

                //获三级分类对象
                List<JSONObject> category3List = categoryViewList1.stream().map(baseCategoryView -> {
                            JSONObject category3 = new JSONObject();
                            category3.put("categoryId", baseCategoryView.getCategory3Id());
                            category3.put("categoryName", baseCategoryView.getCategory3Name());
                            return category3;
                        }
                ).collect(Collectors.toList());
                //三级数据添加到二级分类里
                category2.put("categoryChild", category3List);

                categorxyChild2List.add(category2);
            }
            //二级数据添加到一级分类里
            category1.put("categoryChild", categorxyChild2List);
        }
        // 返回一级分类id
        return category1;
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        //根据一级id获取二级分类id
        List<Long> baseCategory2IdList = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id)).stream().map(BaseCategory2::getId).toList();
        //根据二级分类id获取三级分类对象
        LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getIsTop, 1).in(BaseCategory3::getCategory2Id, baseCategory2IdList).last(" limit 7 ");

        List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(wrapper);

        return  baseCategory3s;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.findAttribute(category1Id);

    }

    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {

        return baseCategoryViewMapper.selectById(category3Id);
    }


}
